package com.jsgt.greekfire.common.util;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsgt.greekfire.R;


public class RecycleEmptyView {


    /**
     * 设置适配器的空布局
     *
     * @param adapter  适配器
     * @param msg      空布局文字提示
     * @param ImgResId 空布局图片资源，若isLoad为true则不生效
     * @param isLoad   是否是加载中
     */
    public static void setAdapterView(AppCompatActivity activity, BaseQuickAdapter adapter, String msg, int ImgResId, boolean isLoad) {
        //声明全局变量View emptyView;
        View emptyView = null;
        if (emptyView == null) {
            emptyView = activity.getLayoutInflater().inflate(R.layout.view_empty, null);
        }
        ((TextView) emptyView.findViewById(R.id.textView_msg)).setText(msg);
        ((ImageView) emptyView.findViewById(R.id.imageView_img)).setImageResource(ImgResId);
       /* if (isLoad) {
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.GONE);
        } else {
            ((ImageView) emptyView.findViewById(R.id.imageView_img)).setImageResource(ImgResId);
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.VISIBLE);
        }*/
        //数据得清空才会显示空布局
        adapter.getData().clear();
        adapter.setEmptyView(emptyView);
        adapter.notifyDataSetChanged();
    }

    /**
     * 显示错误布局
     *
     * @param adapter recyclerView的适配器
     * @param msg     错误信息
     */
    public void showErrorView(AppCompatActivity activity, BaseQuickAdapter adapter, String msg) {
        setAdapterView(activity, adapter, msg, R.mipmap.ic_launcher, false);
    }

    /**
     * 显示空布局
     *
     * @param adapter recyclerView的适配器
     */
    public static void showEmptyView(AppCompatActivity activity, BaseQuickAdapter adapter, String msg, int ImgResId) {
        setAdapterView(activity, adapter, msg, ImgResId, false);
    }

    /**
     * 显示加载中布局
     *
     * @param adapter recyclerView的适配器
     */
    public void showLoadView(AppCompatActivity activity, BaseQuickAdapter adapter) {
        setAdapterView(activity, adapter, "加载中……", 0, true);
    }
}
